package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.service.DataService;
import com.carnation.mong.platform.service.SecurityService;
import com.carnation.mong.platform.um.Group;
import com.carnation.mong.platform.um.Role;
import com.carnation.mong.platform.um.User;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class SecurityServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private SecurityService service;

	public SecurityServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(SecurityService.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.carnation.qa.socketserver.proxy.AbstractServiceImpl#invoke(com.carnation
	 * .qa.socketserver.domain.Item)
	 */
	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("createGroup".equals(method)) {
			returnStr = createGroup(item);
		} else if ("createGroupById".equals(method)) {
			returnStr = createGroupById(item);
		} else if ("createRole".equals(method)) {
			returnStr = createRole(item);
		} else if ("createRoleById".equals(method)) {
			returnStr = createRoleById(item);
		} else if ("createUser".equals(method)) {
			returnStr = createUser(item);
		} else if ("createUserById".equals(method)) {
			returnStr = createUserById(item);
		} else if ("deleteGroup".equals(method)) {
			returnStr = deleteGroup(item);
		} else if ("deleteRole".equals(method)) {
			returnStr = deleteRole(item);
		} else if ("deleteUser".equals(method)) {
			returnStr = deleteUser(item);
		} else if ("getAllGroups".equals(method)) {
			returnStr = getAllGroups(item);
		} else if ("getAllPrivileges".equals(method)) {
			returnStr = getAllPrivileges(item);
		} else if ("getAllRoles".equals(method)) {
			returnStr = getAllRoles(item);
		} else if ("getAllSysPrivileges".equals(method)) {
			returnStr = getAllSysPrivileges(item);
		} else if ("getAllUsers".equals(method)) {
			returnStr = getAllUsers(item);
		} else if ("getAllUsersOfRole".equals(method)) {
			returnStr = getAllUsersOfRole(item);
		} else if ("getDataPrivilegesOfData".equals(method)) {
			returnStr = getDataPrivilegesOfData(item);
		} else if ("getDataPrivlegesOfRole".equals(method)) {
			returnStr = getDataPrivlegesOfRole(item);
		} else if ("getGroup".equals(method)) {
			returnStr = getGroup(item);
		} else if ("getGroupsOfUser".equals(method)) {
			returnStr = getGroupsOfUser(item);
		} else if ("getPrivilegesOfRole".equals(method)) {
			returnStr = getPrivilegesOfRole(item);
		} else if ("getPrivilegesOfUser".equals(method)) {
			returnStr = getPrivilegesOfUser(item);
		} else if ("getRole".equals(method)) {
			returnStr = getRole(item);
		} else if ("getRolesOfGroup".equals(method)) {
			returnStr = getRolesOfGroup(item);
		} else if ("getRolesOfUser".equals(method)) {
			returnStr = getRolesOfUser(item);
		} else if ("getRootGroups".equals(method)) {
			returnStr = getRootGroups(item);
		} else if ("getSubGroups".equals(method)) {
			returnStr = getSubGroups(item);
		} else if ("getSysPrivilegesOfRole".equals(method)) {
			returnStr = getSysPrivilegesOfRole(item);
		} else if ("getSysPrivilegesOfUser".equals(method)) {
			returnStr = getSysPrivilegesOfUser(item);
		} else if ("getUser".equals(method)) {
			returnStr = getUser(item);
		} else if ("getUsersOfGroup".equals(method)) {
			returnStr = getUsersOfGroup(item);
		} else if ("getUsersOfRole".equals(method)) {
			returnStr = getUsersOfRole(item);
		} else if ("modifyPassword".equals(method)) {
			returnStr = modifyPassword(item);
		} else if ("updateDataPrivileges".equals(method)) {
			returnStr = updateDataPrivileges(item);
		} else if ("updateGroup".equals(method)) {
			returnStr = updateGroup(item);
		} else if ("updateGroupById".equals(method)) {
			returnStr = updateGroupById(item);
		} else if ("updateRole".equals(method)) {
			returnStr = updateRole(item);
		} else if ("updateRoleById".equals(method)) {
			returnStr = updateRoleById(item);
		} else if ("updateUser".equals(method)) {
			returnStr = updateUser(item);
		} else if ("updateUserById".equals(method)) {
			returnStr = updateUserById(item);
		} else if ("updateUserInfo".equals(method)) {
			returnStr = updateUserInfo(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	@SuppressWarnings("unchecked")
	private String createGroup(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Group group = instanceGroup(item.getChildren().get(0));
			List users = instanceList(item.getChildren().get(1));
			List roles = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("group:" + group + ",users:" + users + ",roles:"
						+ roles);
			}
			try {
				Object obj = service.createGroup(group, users, roles);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String createGroupById(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Group group = instanceGroup(item.getChildren().get(0));
			List userIds = instanceList(item.getChildren().get(1));
			List roleIds = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("group:" + group + ",userIds:" + userIds
						+ ",roleIds:" + roleIds);
			}
			try {
				Object obj = service.createGroupById(group, userIds, roleIds);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String createRole(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Role role = instanceRole(item.getChildren().get(0));
			List privileges = instanceList(item.getChildren().get(1));
			List sysPrivileges = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("role:" + role + ",privileges:" + privileges
						+ ",sysPrivileges:" + sysPrivileges);
			}
			try {
				Object obj = service
						.createRole(role, privileges, sysPrivileges);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String createRoleById(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Role role = instanceRole(item.getChildren().get(0));
			List privilegeIds = instanceList(item.getChildren().get(1));
			List sysPrivilegeIds = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("role:" + role + ",privilegeIds:" + privilegeIds
						+ ",sysPrivilegeIds:" + sysPrivilegeIds);
			}
			try {
				Object obj = service.createRoleById(role, privilegeIds,
						sysPrivilegeIds);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String createUser(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			User user = instanceUser(item.getChildren().get(0));
			List groups = instanceList(item.getChildren().get(1));
			List roles = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("user:" + user + ",groups:" + groups + ",roles:"
						+ roles);
			}
			try {
				Object obj = service.createUser(user, groups, roles);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String createUserById(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			User user = instanceUser(item.getChildren().get(0));
			List groupIds = instanceList(item.getChildren().get(1));
			List roleIds = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("user:" + user + ",groupIds:" + groupIds
						+ ",roleIds:" + roleIds);
			}
			try {
				Object obj = service.createUserById(user, groupIds, roleIds);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String deleteGroup(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("id:" + id);
			}
			Object obj;
			try {
				service.deleteGroup(id);
				ServiceFactory.commit();
				if (service.getGroup(id) == null) {
					obj = true;
				} else {
					obj = false;
				}
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String deleteRole(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("id:" + id);
			}
			Object obj;
			try {
				service.deleteRole(id);
				ServiceFactory.commit();
				if (service.getRole(id) == null) {
					obj = true;
				} else {
					obj = false;
				}
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String deleteUser(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("id:" + id);
			}
			Object obj;
			try {
				service.deleteUser(id);
				ServiceFactory.commit();
				if (service.getUser(id) == null) {
					obj = true;
				} else {
					obj = false;
				}
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getAllGroups(Item item) {
		Object obj;
		try {
			obj = service.getAllGroups();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getAllPrivileges(Item item) {
		Object obj;
		try {
			obj = service.getAllPrivileges();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getAllRoles(Item item) {
		Object obj;
		try {
			obj = service.getAllRoles();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getAllSysPrivileges(Item item) {
		Object obj;
		try {
			obj = service.getAllSysPrivileges();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getAllUsers(Item item) {
		Object obj;
		try {
			obj = service.getAllUsers();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getAllUsersOfRole(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("roleId:" + id);
			}
			Object obj;
			try {
				obj = service.getAllUsersOfRole(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getDataPrivilegesOfData(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			String id = (String) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("dataRDN:" + id);
			}
			Object obj;
			try {
				obj = service.getDataPrivilegesOfData(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getDataPrivlegesOfRole(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("roleId:" + id);
			}
			Object obj;
			try {
				obj = service.getDataPrivilegesOfRole(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getGroup(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("groupId:" + id);
			}
			Object obj;
			try {
				obj = service.getGroup(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		} else if (item.getChildren().size() == 2) {
			String name = (String) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			Long id = (Long) getObject(item.getChildren().get(1)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("name:" + name + ",parentId:" + id);
			}
			Object obj;
			try {
				obj = service.getGroup(name, id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getGroupsOfUser(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("userId:" + id);
			}
			Object obj;
			try {
				obj = service.getGroupsOfUser(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getPrivilegesOfRole(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("roleId:" + id);
			}
			Object obj;
			try {
				obj = service.getPrivilegesOfRole(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getPrivilegesOfUser(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("userId:" + id);
			}
			Object obj;
			try {
				obj = service.getPrivilegesOfUser(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getRole(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Object id = getObject(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug(id);
			}
			Object obj;
			try {
				if (id instanceof String) {
					obj = service.getRole((String) id);
				} else {
					obj = service.getRole((Long) id);
				}
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getRolesOfGroup(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("groupId:" + id);
			}
			Object obj;
			try {
				obj = service.getRolesOfGroup(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getRolesOfUser(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("userId:" + id);
			}
			Object obj;
			try {
				obj = service.getRolesOfUser(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getRootGroups(Item item) {
		Object obj;
		try {
			obj = service.getRootGroups();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getSubGroups(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("groupId:" + id);
			}
			Object obj;
			try {
				obj = service.getSubGroups(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getSysPrivilegesOfRole(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("roleId:" + id);
			}
			Object obj;
			try {
				obj = service.getSysPrivilegesOfRole(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getSysPrivilegesOfUser(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("userId:" + id);
			}
			Object obj;
			try {
				obj = service.getSysPrivilegesOfUser(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getUser(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Object id = getObject(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug(id);
			}
			Object obj;
			try {
				if (id instanceof String) {
					obj = service.getUser((String) id);
				} else {
					obj = service.getUser((Long) id);
				}
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getUsersOfGroup(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("groupId:" + id);
			}
			Object obj;
			try {
				obj = service.getUsersOfGroup(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getUsersOfRole(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("roleId:" + id);
			}
			Object obj;
			try {
				obj = service.getUsersOfRole(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String modifyPassword(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Long id = (Long) getObject(item.getChildren().get(0)
					.getLabelParts()[1]);
			String oldPassword = (String) getObject(item.getChildren().get(1)
					.getLabelParts()[1]);
			String newPassword = (String) getObject(item.getChildren().get(2)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("userId:" + id + ",oldPassword:" + oldPassword
						+ ",newPassword:" + newPassword);
			}
			Object obj;
			try {
				service.modifyPassword(id, oldPassword, newPassword);
				ServiceFactory.commit();
				obj = service.getUser(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String updateDataPrivileges(Item item) {
		if (item.getChildren().size() == 2) {// 参数个数
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			List dataPrivileges = instanceList(item.getChildren().get(1));
			if (log.isDebugEnabled()) {
				log.debug("dataId:" + dataId + ",dataPrivileges:"
						+ dataPrivileges);
			}
			try {
				service.updateDataPrivileges(dataId, dataPrivileges);
				ServiceFactory.commit();
				Object obj = ServiceFactory.getService(DataService.class)
						.getData(dataId);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String updateGroup(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Group group = instanceGroup(item.getChildren().get(0));
			List users = instanceList(item.getChildren().get(1));
			List roles = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("group:" + group + ",users:" + users + ",roles:"
						+ roles);
			}
			try {
				service.updateGroup(group, users, roles);
				ServiceFactory.commit();
				Object obj = service.getGroup(group.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String updateGroupById(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Group group = instanceGroup(item.getChildren().get(0));
			List userIds = instanceList(item.getChildren().get(1));
			List roleIds = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("group:" + group + ",userIds:" + userIds
						+ ",roleIds:" + roleIds);
			}
			try {
				service.updateGroupById(group, userIds, roleIds);
				ServiceFactory.commit();
				Object obj = service.getGroup(group.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String updateRole(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Role group = instanceRole(item.getChildren().get(0));
			List privileges = instanceList(item.getChildren().get(1));
			List sysPrivileges = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("group:" + group + ",privileges:" + privileges
						+ ",sysPrivileges:" + sysPrivileges);
			}
			try {
				service.updateRole(group, privileges, sysPrivileges);
				ServiceFactory.commit();
				Object obj = service.getGroup(group.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String updateRoleById(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			Role role = instanceRole(item.getChildren().get(0));
			List privilegeIds = instanceList(item.getChildren().get(1));
			List sysPrivilegeIds = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("role:" + role + ",privilegeIds:" + privilegeIds
						+ ",sysPrivilegeIds:" + sysPrivilegeIds);
			}
			try {
				service.updateRoleById(role, privilegeIds, sysPrivilegeIds);
				ServiceFactory.commit();
				Object obj = service.getRole(role.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String updateUser(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			User user = instanceUser(item.getChildren().get(0));
			List groups = instanceList(item.getChildren().get(1));
			List roles = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("user:" + user + ",groups:" + groups + ",roles:"
						+ roles);
			}
			try {
				service.updateUser(user, groups, roles);
				ServiceFactory.commit();
				Object obj = service.getUser(user.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	@SuppressWarnings("unchecked")
	private String updateUserById(Item item) {
		if (item.getChildren().size() == 3) {// 参数个数
			User user = instanceUser(item.getChildren().get(0));
			List groupIds = instanceList(item.getChildren().get(1));
			List roleIds = instanceList(item.getChildren().get(2));
			if (log.isDebugEnabled()) {
				log.debug("user:" + user + ",groupIds:" + groupIds
						+ ",roleIds:" + roleIds);
			}
			try {
				service.updateUserById(user, groupIds, roleIds);
				ServiceFactory.commit();
				Object obj = service.getUser(user.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String updateUserInfo(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			User user = instanceUser(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("user:" + user);
			}
			try {
				service.updateUserInfo(user);
				ServiceFactory.commit();
				Object obj = service.getUser(user.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

}
